package com.mini.controller;

//import com.mini.service.MnsService;
import com.mini.mysql.service.MnsService;
import com.mini.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisCommands;

/**
 * @author zhangxiaobin
 * @title: BaseController
 * @description: TODO
 * @date 2019/10/2711:35
 */
@Slf4j
public class BaseController {

    @Autowired
    RedisTemplate redisTemplate;

//    @Autowired
//    protected BusinessStudentInfoService studentInfoService;

    @Autowired
    protected MnsService miniAdminService;


    @Value("${mini.eureka.call.sign}")
    String sign;



    public UserVO getLoginUser(String token) {
        return miniAdminService.getLoginUser(token);
    }

    public Long getLoginUserId(String token) {
        return miniAdminService.getLoginUserId(token);
    }
    public Boolean isLogin(String token) {
        return miniAdminService.isLogin(token);
    }

    public boolean checkAuth(String token){
        try {
            UserVO loginUser = getLoginUser(token);
            if(loginUser != null){
                return true;
            }
            return false;
        }catch (Exception ex){
            log.error(" login get user error , {} ",ex.getMessage());
            return false;
        }
    }

    protected boolean tryLock(String key,long time){
        try {
            String result = (String) redisTemplate.execute((RedisCallback<String>) connection -> {
                JedisCommands commands = (JedisCommands) connection.getNativeConnection();
                return commands.set(key, key, "NX", "EX", time);
            });
            return !StringUtils.isEmpty(result);
        } catch (Exception e) {
            log.error("getNewUsePushLock error", e);
        }
        return false;
    }

    protected boolean tryLock(String key){
        try {
            String result = (String) redisTemplate.execute((RedisCallback<String>) connection -> {
                JedisCommands commands = (JedisCommands) connection.getNativeConnection();
                return commands.set(key, key, "NX", "EX", 5);
            });
            return !StringUtils.isEmpty(result);
        } catch (Exception e) {
            log.error("getNewUsePushLock error", e);
        }
        return false;
    }

    protected void releaseLockKey(String key){
        try {
            redisTemplate.delete(key);
        } catch (Exception e) {
            log.error("releaseNewUsePushLock error", e);
        }
    }


    protected boolean checkSign(String requestSig){
        if(StringUtils.isBlank(requestSig)){
            return false;
        }
        if(sign.equalsIgnoreCase(requestSig)){
            return true;
        }
        return false;
    }


}
